﻿using ConfExpert.Utility;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Base;
using Extensions;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfOrder
{
    public partial class Warehouse : XForm
    {
        private class Owner
        {
            public int OwnerId;
            public string Name;

            public Owner(int ownerId, string name)
            {
                this.OwnerId = ownerId;
                this.Name = name;
            }
            public override string ToString()
            {
                return Name;
            }
        }

        public Warehouse()
        {
            InitializeComponent();
        }

        private void Warehouse_Load(object sender, EventArgs e)
        {
            MainToolbar.Visible = true;
            gleCustomer.Edit.Assign(RepositoryManager.riCustomer);

            DataTable dtOwner = new DataTable();
            dtOwner.Columns.Add("OwnerId", typeof(string));
            dtOwner.Columns.Add("Name", typeof(string));
            dtOwner.Rows.Add("", "ALL");
            dtOwner.Rows.Add("1", "KARBEL");
            dtOwner.Rows.Add("75", "WESTBROOK");
            gleOwner.Edit.Assign(RepositoryManager.GetGLESablon(dtOwner, "Name", "OwnerId"));

            gleOwner.EditValue = "";

            gleCustomer.EditValueChanged += gleCustomer_EditValueChanged;
            gleOwner.EditValueChanged += gleCustomer_EditValueChanged;

            gleCustomer.EditValue = AppSession.UserInfo.CustomerId;
            splitter.SizeChanged += MySizeChanged;
        }

        void gleCustomer_EditValueChanged(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            gvArticle.FocusedRowChanged -= gridView1_FocusedRowChanged;

            gvArticle.Init("COGetStok", gleCustomer.EditValue.ToIntZero(), gleOwner.EditValue.ToIntZero());
            //gvArticle.SetCaptions("PhotoMikro:.");

            gvArticle.AddDetailGrid(gvRoll, "ProductId");
            gvArticle.MakeColumnInvisible("ProductId");

            gvRoll.SutunEkle("LineTotal", typeof(decimal), "UnitPrice*Qty");
            gvRoll.SutunEkle("Q", typeof(decimal), "Iif(UnitPrice>0, Qty, 0)");
            gvArticle.SutunEkle("AvgPrice", typeof(decimal), "SUM(Child({FK}).LineTotal)/(SUM(Child({FK}).Q)+0.0001)");

            gvArticle.FormatAsNumber(2, "AvgPrice", "InvoicePrice", "MaxUnitPrice");
            gvRoll.FormatAsNumber(2, "UnitPrice");
            gvRoll.MakeColumnInvisible("Scan", "ProductId", "LineTotal", "Q");

            gvArticle.FormatAsNumber(1, "QtyIDLE", "QtyRsvd", "TotalQty", "InvoiceTotal");
            gvArticle.AddSummary("QtyIDLE", "QtyRsvd", "TotalQty", "InvoiceTotal");
            gvRoll.FormatAsNumber(1, "Qty");
            gvRoll.AddSummary("Qty");

            gvArticle.MakeReadOnly();
            gvRoll.MakeReadOnly();

            gvArticle.BestFitColumns();
            gvRoll.BestFitColumns();

            this.Cursor = Cursors.Default;
            gvArticle.FocusedRowChanged += gridView1_FocusedRowChanged;
        }

        void gridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            gvRoll.BestFitColumns();
        }
    }
}
